package cn.gxa.edu_adm.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringUtils {

    // 判断字符是否为空

    /**
     *   用户  输入  ""   返回 null
     *   用户  输入  "   "   返回 null
     *   用户  输入  "  aaa  "   返回 "aaa"
     * @param str
     */
    static     public   String  formatString(String   str){//""  null  "  aaa  "  aaa
         if(str!=null  &&  str.trim().length()>0){
             return   str.trim();
         }
         return   null;
    }

    static     public   Integer  formatInteger(String   str){//""  null  "  aaa  "  aaa
        if(str!=null  &&  str.trim().length()>0){
            try{
                return  Integer.parseInt(str);
            }catch (Exception  e){
                return null;
            }
        }
        return   null;
    }

    static     public   Double  formatDouble(String   str){//""  null  "  aaa  "  aaa
        return formatDouble(str,-1);
    }
    static     public   Double  formatDouble(String   str,int decimal_Places){//""  null  "  aaa  "  aaa

        if(str!=null  &&  str.trim().length()>0){
            try{
                double  number=Double.parseDouble(str);
                if(decimal_Places>-1){
                    //保留decimal_Places位小数
                    number= Double.parseDouble(String.format("%."+decimal_Places+"f",number));
                }
                return  number;

            }catch (Exception  e){
                System.out.println(e);
                return null;
            }
        }
        return   null;
    }

    static     public Date formatDate(String   str){//""  null  "  aaa  "  aaa
        return formatDate(str,null);
    }

    static     public Date formatDate(String   str,String  pattern){//""  null  "  aaa  "  aaa
        if(pattern==null){
            pattern="yyyy-MM-dd";
        }
        SimpleDateFormat    simpleDateFormat=new SimpleDateFormat(pattern);
        try {
            return   simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return   null;
    }

    static     public String formatString(Date   time){//""  null  "  aaa  "  aaa
        return formatString(time,null);
    }

    static     public String formatString(Date   time,String  pattern){//""  null  "  aaa  "  aaa
        if(pattern==null){
            pattern="yyyy-MM-dd";
        }
        SimpleDateFormat    simpleDateFormat=new SimpleDateFormat(pattern);
        try {
            return   simpleDateFormat.format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return   null;

    }

    //判断字符是否是数字
    //判断字符是否日期
    //判断字符串
}
